package de.gwtprime.server.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.utils.SystemProperty;
import com.google.web.bindery.requestfactory.server.RequestFactoryServlet;

import de.gwtprime.server.model.User2;
import de.gwtprime.server.sql.helper.HibernateHelper;
import de.gwtprime.shared.util.AppUtilsShared;

/**
 * Helper methods for the server side
 */
public class AppUtils {
	
	/**
	 * Use this string to salt the password hashes
	 */
	public static String hashSalt = "asdfj37z5_Sfxvmxy,vn!!!))KDFJHKLFHDJÖJM;";
	
	public static User2 userInSessionForUnitTests = null;

	public enum SessionFields{
		
		User("user"),
		Test("test");
		
		private String name;
		
		SessionFields(String name){
			this.name = name;
		}
		
		@Override
		public String toString() {
			return name;
		}
		
	}
	
	/**
	 * Check if currently running on app engine
	 * @return
	 */
	public static boolean isAppEngine(){
		return SystemProperty.environment.value() ==
			    SystemProperty.Environment.Value.Production;
	}
	
	public static String getHostUrl(){
		return isAppEngine() ? AppUtilsShared.DOMAIN_PROD : AppUtilsShared.DOMAIN_LOCAL;
	}
	
	public static User2 getUserFromSession() {
		
		if(HibernateHelper.test){
			return userInSessionForUnitTests;
		}
		
		HttpSession session = RequestFactoryServlet.getThreadLocalRequest().getSession();
		
		User2 u = (User2)session.getAttribute("user");
		return u;
	}
	
	static public String getApplitionURL(HttpServletRequest request) {

		if (ServletHelper.isDevelopment(request)) {
			return "http://127.0.0.1:8888/BitcoinBoerse.html?gwt.codesvr=127.0.0.1:9997";
		} else {
			return ServletUtils.getBaseUrl(request);
		}

	}
}
